<?php
            
use app\model\Record; 
use app\model\Koi; 


/**
 * Desc:
 * Time: 2021/5/27 1:35 下午
 */
require_once PATH_MODULE .'define.php';
class LingzeModel extends PluginModel {
    //获取奖项列表
    public static function getPrizeList($themeid,$chest_name='',$status=0,$level_sort=0,$page_start=0,$page_size=10)
    {
        $condition = "";
        if ($chest_name) {
            $condition .= " and chest_name='".$chest_name."'";
        }
        if ($status) {
            $condition .= " and status=$status";
        }
        if ($level_sort) {
            $condition .= " and level_sort='".$level_sort."'";
        }
        $condition .= " order by created_at desc ";
        if ($page_size){
            $condition .= "  limit ".$page_start.",".$page_size;
        }
        $sql = " SELECT * FROM ".tablename(yx_lz_prize)." WHERE themeid=$themeid".$condition;
        return db_select($sql);
    }

    //获取小于某关卡的记录
    public static function getPrizeListBySort($themeid,$sort)
    {
        $sql = " SELECT * FROM ".tablename(yx_lz_prize)." WHERE themeid=$themeid and level_sort<$sort";
        return db_select($sql);
    }
    //插入奖项
    public static function insertPrize($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(yx_lz_prize,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }
    //修改奖项
    public static function editPrizeItemByWhere($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(yx_lz_prize,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }
    //获取奖项
    public static function getPrizeItemById($id,$themeid)
    {
        $res = db_get(yx_lz_prize,['lc_id'=>$id,'themeid'=>$themeid]);
        return $res;
    }

    public static function getPrizeItemByWhere($where)
    {
        $res = db_get(yx_lz_prize,$where);
        return $res;
    }
    //获取全部奖品
    public static function getAllPrize($themeid)
    {
        $res = db_getall(yx_lz_prize,['themeid'=>$themeid]);
        return $res;
    }

    //获取当前次数对应的最大关卡信息
    public static function getPrizeItemByOpNum($themeid,$op,$op_num)
    {
        $sql = " select * from ".tablename(yx_lz_prize). " where themeid=$themeid and type='".$op."' and type_num <=$op_num order by level_sort desc ";
        return db_find($sql);
    }
    //通过当前关卡 操作次数  操作类型
    public static function getPrizeItemByLevelSortAndOpNum($themeid,$level_sort)
    {
        $sql = " select * from ".tablename(yx_lz_prize). " where themeid=$themeid and level_sort=$level_sort";
        return db_find($sql);
    }
    //获取下一关同类型的操作总次数
    public static function getPrizeItemBySortTotalOpNum($themeid,$prize_item)
    {
        $op = $prize_item['type'];
        $level_sort = $prize_item['level_sort'];
        $sql = "select sum(type_num) as cnt from " . tablename(yx_lz_prize)." where type='".$op."' and themeid=$themeid"." and level_sort<= $level_sort";
        return intval(db_find($sql)['cnt']);
    }
    //通过关卡获取奖品
    public static function getPrizeItemBySort($sort,$themeid)
    {
        $res = db_get(yx_lz_prize,['level_sort'=>$sort,'themeid'=>$themeid]);
        return $res;
    }
    //搜索奖项
    public static function searchPrize($themeid,$question,$page_start,$page_size)
    {
        $condition="";
        if (!empty($question)) {
            $condition .= " AND chest_name LIKE '%".$question."%'"." OR box_name like '%".$question."%'";
        }
        $condition .= " GROUP BY lc_id ORDER BY created_at DESC  ";
        if ($page_size) {
            $condition .= " LIMIT " .$page_start.",".$page_size;
        }
        $sql = " SELECT * FROM ".tablename(yx_lz_prize)." WHERE themeid=$themeid".$condition;
        return db_select($sql);
    }
    //删除奖项
    public static function deletePrize($lc_id,$themeid)
    {
        return db_delete(yx_lz_prize,['lc_id'=>$lc_id,'themeid'=>$themeid]);
    }

    //更新活动粉丝表信息
    public static function updateFansInfo($uid,$update_data)
    {
        $res = db_update(t_fans,$update_data,['uid'=>$uid]);
        if (!$res) {
            return false;
        } else {
            return true;
        }
    }

    public static function getUserInfoByUid($uid)
    {
        $res = db_get(t_fans,['uid'=>$uid]);
        return $res;
    }


    //搜索中奖记录
    public static function searchDefaultLt($themeid,$search='',$page_start=0,$page_size=10,$start_day='',$end_day='')
    {
        $condition="";
        if ($search){
            $condition .= " AND b.nickname LIKE '".$search."'"." or b.mobile LIKE '".$search."' "." or c.chest_name LIKE '".$search."' ".
            " or c.box_name LIKE '".$search."' ";
        }
        if ($start_day && $end_day && $start_day!=$end_day){
            $condition .= "  and a.created_at between '".$start_day."' and '".$end_day."'";
        }
        $condition .= " ORDER BY a.created_at DESC ";
        if ($page_size) {
            $condition.= "LIMIT ".$page_start.",".$page_size;
        }
        $sql = " SELECT a.*,b.avatar,b.nickname,b.username,b.mobile,c.chest_name,c.box_name,c.level_sort,c.small_img FROM ".tablename(yx_lz_flag)." AS  a ".
            " LEFT JOIN ".tablename(t_fans)." AS b ON a.uid=b.uid and b.themeid=$themeid".
            " LEFT JOIN ".tablename(yx_lz_prize)." AS c ON a.lc_id=c.lc_id and c.themeid=$themeid".
            " WHERE a.themeid=$themeid".$condition;
        return db_select($sql);
    }
    //获取指定的中奖记录信息
    public static function getLtFlagItem($themeid,$uid,$lc_id)
    {
        $res = db_get(yx_lz_flag,['lc_id'=>$lc_id,'themeid'=>$themeid,'uid'=>$uid]);
        return $res;
    }
    //获取用户的全部中奖记录
    public static function getAllFlag($themeid,$uid)
    {
        $res = db_getall(yx_lz_flag,['themeid'=>$themeid,'uid'=>$uid]);
        return $res;
    }

    //修改中奖记录
    public static function editLtFlagItem($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(yx_lz_flag,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }
    //插入中奖记录
    public static function insertLtFlag($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(yx_lz_flag,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }

    //获取奖项中奖记录
    public static function getDefaultLtList($themeid,$status=0,$page_start=0,$page_size=10)
    {
        $condition = "";
        if ($status) {
            $condition .= " AND a.status=$status";
        }
        $condition .= " ORDER BY a.f_id DESC ";
        if ($page_size) {
            $condition .= " LIMIT ".$page_start.",".$page_size;
        }
        $sql = " SELECT a.*,b.avatar,b.nickname,b.username,b.mobile,c.chest_name,c.box_name,c.level_sort,c.small_img FROM ".tablename(yx_lz_flag)." AS  a ".
            " LEFT JOIN ".tablename(t_fans)." AS b ON a.uid=b.uid and b.themeid=$themeid".
            " LEFT JOIN ".tablename(yx_lz_prize)." AS c ON a.lc_id=c.lc_id".
            " WHERE a.themeid=$themeid".$condition;
        return db_select($sql);
    }


    //获取用户实例
    public static function getUserEntry($uid,$themeid)
    {
        $res = db_get(yx_lz_chest_entry,['uid'=>$uid,'themeid'=>$themeid]);
        return $res;
    }
    //插入用户实例
    public static function insertEntry($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(yx_lz_chest_entry,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }
    //修改用户实例
    public static function editEntry($where,$data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = db_update(yx_lz_chest_entry,$data,$where);
        if (!empty($res)) {
            return true;
        } else {
            return false;
        }
    }

    //插入操作记录
    public static function insertOpFlag($data)
    {
        $data['updated_at'] = date("Y-m-d H:i:s");
        $res = $insertid = db_insert(yx_lz_op_flag,$data);
        if (!empty($res)) {
            return $insertid;
        } else {
            return false;
        }
    }
    //获取对应类型的操作记录数
    public static function getOpFlagNumByType($fromuid,$themeid,$op)
    {
        $sql = "select count(*) as cnt from " . tablename(yx_lz_op_flag)." where uid=".$fromuid." and type='".$op."' and themeid=".$themeid;
        return intval(db_find($sql)['cnt']);
    }

    //获取指定时间节点前的三条操作记录
    public static function getThirdOpFlagByFiled($themeid,$op,$uid,$time,$type_num)
    {
        if ($type_num>3){
            $type_num = 3;
        }
        $sql = " SELECT a.*,b.avatar,b.nickname,b.username,b.mobile FROM ".tablename(yx_lz_op_flag)." AS  a ".
            " LEFT JOIN ".tablename(t_fans)." AS b ON a.from_uid=b.uid and b.themeid=a.themeid ".
            " where a.uid=".$uid." and a.themeid=".$themeid.
            " and a.type='".$op."' and a.created_at <= '".$time."' order by a.c_id desc limit $type_num";
        return db_select($sql);
    }

    public static function formatGetThirdOpFlag($themeid,$op,$uid,$time,$type_num)
    {
        $arr = [];
        $list = self::getThirdOpFlagByFiled($themeid,$op,$uid,$time,$type_num);
        foreach ($list as $k => $v)
        {
            if ($k<2){
                $temp = ['avatar'=>$v['avatar'],'nickname'=>$v['nickname'],'username'=>$v['username']];
                array_push($arr,$temp);
            }else{
                $temp = ['avatar'=>'https://dpy01cdn.yixuan.net/images/2/2021/05/DTlcPnc6h6K6kl2lt2kHtXMaxOa6C0.png','nickname'=>'','username'=>''];
                array_push($arr,$temp);
                break;
            }
        }
        return $arr;
    }

    public static function getFriendHasViewTotal($themeid,$uid,$from_uid,$op)
    {
        $sql = "select count(*) as cnt from " . tablename(yx_lz_op_flag)." where from_uid=".$from_uid." and uid='{$uid}' and type='".$op."' and themeid=".$themeid;
        return intval(db_find($sql)['cnt']);
    }

    public static function initCheck($theme,$config,$fans,$fromuid,$themyrecord)
    {
        try {
            $themeid = $theme['id'];
            $fans_entry = self::getUserEntry($fans['uid'],$themeid);
            if ($fans['uid']!=$fromuid && $fromuid && !$themyrecord){
                $themyrecord = Record::getLastGroupByUid($fromuid,$themeid);
                $fans_entry = self::getUserEntry($fromuid,$themeid);
                if ($fans_entry && $themyrecord){
                    //好友到访
                    //插入好友到访记录
                    $num = self::getFriendHasViewTotal($themeid,$fromuid,$fans['uid'],'friend_view');
                    if (!$num){
                        self::ApiAddOptionFlag($fromuid,$fans['uid'],$themeid,'friend_view');
                    }
                    $fromfans = self::getUserInfoByUid($fromuid);
                    //幸运星是否收集手机号 逻辑判断
                    $lingze_get_mobile = 0;
                    if (empty($fans['mobile']) && $config['lz_get_phone']){
                        $lingze_get_mobile = 1;
                    }
                    $format_res = [
                        'error_txt'=>'',
                        'show_id'=>6,
                        'info'=>[
                            'first_title'=>empty($fromfans['username'])?'您的朋友'.$fromfans['nickname'].'已经':'您的朋友'.$fromfans['username'].'已经',
                            'second_title'=>'成功抢购',
                            'first_content'=>$config['lz_package_title'],
                            'avatar'=>$fromfans['avatar'],
                            'list'=>self::getFormatUserBox($themeid,$fromuid,true,$config,$themyrecord),
                            'btn_txt'=>'我也参加活动',
                            'lingze_get_mobile'=>(int)$lingze_get_mobile
                        ]
                    ];
                }
            }else{
                if (!$themyrecord){
                    $format_res = [
                        'error_txt'=>'未下单'
                    ];
                }else{
                    //判断用户是否初始化
                    if (!$fans_entry){
                        //用户未点击立即参与
                        //获取第一关奖品
                        $first_level_prize = self::getPrizeItemBySort(1,$theme['id']);
                        if (!$first_level_prize){
                            $format_res = [
                                'error_txt'=>'后台未配置第一关奖品'
                            ];
                        }else{
                            $format_res = [
                                'error_txt'=>'',
                                'show_id'=>1,
                                'info'=>[
                                    'title'=>'获得'.$first_level_prize['box_name'],
                                    'content'=>$first_level_prize['chest_name'],
                                    'btn_txt'=>'开宝箱'
                                ]
                            ];
                        }
                    }else{
                        self::checkLevelFlagAndPreFlag($themeid,$fans['uid'],'','',$fans_entry['lz_chest_sort']);

                        $fans_entry = self::getUserEntry($fans['uid'],$themeid);
                        $lz_chest_sort = $fans_entry['lz_chest_sort'];
                        if (!$lz_chest_sort){
                            $first_level_prize = self::getPrizeItemBySort(1,$themeid);
                            $format_res = [
                                'error_txt'=>'',
                                'show_id'=>2,
                                'info'=>[
                                    'first_title'=>self::getFirstTitle($fans['uid'],$first_level_prize),
                                    'second_title'=>'即可开启宝箱',
                                    'first_content'=>'100%',
                                    'second_content'=>'中奖',
                                    'img'=>$first_level_prize['small_img'],
                                    'box_name'=>$first_level_prize['box_name'],
                                    'chest_name'=>$first_level_prize['chest_name'],
                                    'btn_txt'=>'邀请好友开启宝箱'
                                ]
                            ];
                        }else{
                            $box_total = count(self::getAllFlag($themeid,$fans['uid']));
                            $prize_total = count(self::getAllPrize($themeid));
                            $title = self::getHasBoxTitle($themeid,$box_total,$lz_chest_sort,$prize_total,$fans_entry);
                            $btn_txt = '邀请好友开启宝箱';
                            if ($box_total>=$prize_total){
                                $btn_txt= '邀请好友参与活动';
                            }
                            $format_res = [
                                'error_txt'=>'',
                                'show_id'=>4,
                                'info'=>[
                                    'first_title'=>$title['first_title'],
                                    'second_title'=>$title['second_title'],
                                    'first_content'=>'我的宝箱',
                                    'list'=>self::getFormatUserBox($themeid,$fans['uid']),
                                    'btn_txt'=>$btn_txt
                                ]
                            ];
                        }
                    }
                }
            }

            return $format_res;
        }catch (Exception $e){
            var_dump($e->getMessage());die();
        }
    }

    //首次进入 不存宝箱
    public static function getFirstTitle($uid,$first_level_prize)
    {
        $arr = [];
        //判断用户存在操作记录
        switch ($first_level_prize['type']){
            case 'share':
                $now_op_flag = self::getOpFlagNumByType($uid,$first_level_prize['themeid'],'share');
                $new_type_num = max(0,intval($first_level_prize['type_num']-$now_op_flag));
                $arr=['first'=>'分享','second'=>$new_type_num.'位','third'=>'好友即可开启宝箱'];
                break;
            case 'friend_view':
                $now_op_flag = self::getOpFlagNumByType($uid,$first_level_prize['themeid'],'friend_view');
                $new_type_num = max(0,intval($first_level_prize['type_num']-$now_op_flag));
                $arr=['first'=>'邀请','second'=>$new_type_num.'位','third'=>'好友到访即可开启宝箱'];
                break;
            case 'friend_order':
                $now_op_flag = self::getOpFlagNumByType($uid,$first_level_prize['themeid'],'friend_order');
                $new_type_num = max(0,intval($first_level_prize['type_num']-$now_op_flag));
                $arr=['first'=>'邀请','second'=>$new_type_num.'位','third'=>'好友下单即可开启宝箱'];
                break;
            default:
                break;
        }
        return $arr;
    }

    public static function getHasBoxTitle($themeid,$count,$lz_chest_sort,$prize_total,$fans_entry)
    {
        $next_sort = intval($lz_chest_sort+1);
        $next_prize = self::getPrizeItemBySort($next_sort,$themeid);
        if (!$next_prize){
            $next_prize = self::getPrizeItemBySort($lz_chest_sort,$themeid);
        }
        $current_sort = intval($next_sort-1);
        if ($count<$prize_total){
            $arr = [
                'first'=>'恭喜您，已开启',
                'second'=>$count.'个',
                'third'=>'宝箱'
            ];
        }else{
            $arr = [
                'first'=>'恭喜您，已开启全部宝箱',
                'second'=>'',
                'third'=>''
            ];
        }
        $next_prize_type_num = intval($next_prize['type_num']);
        //获取用户的实例信息
        //获取下一关之前的同类型的关卡的操作次数总和
        $common_type_prizes_total = (int)self::getPrizeItemBySortTotalOpNum($themeid,$next_prize);
        $arr1 = [];
        switch ($next_prize['type']){
            case 'share':
                $next_prize_type_num = intval($common_type_prizes_total-$fans_entry['share_num']);
                $arr1 = [
                    'first'=>'再分享',
                    'second'=>$next_prize_type_num.'位',
                    'third'=>'好友可继续开启'.$next_prize['box_name']
                ];
                break;
            case 'friend_view':
                $next_prize_type_num = intval($common_type_prizes_total-$fans_entry['friend_view_num']);
                $arr1 = [
                    'first'=>'再邀请',
                    'second'=>$next_prize_type_num.'位',
                    'third'=>'好友到访可继续开启'.$next_prize['box_name']
                ];
                break;
            case 'friend_order':
                $next_prize_type_num = intval($common_type_prizes_total-$fans_entry['friend_order_num']);
                $arr1 = [
                    'first'=>'再邀请',
                    'second'=>$next_prize_type_num.'位',
                    'third'=>'好友下单可继续开启'.$next_prize['box_name']
                ];
                break;
            default:
                break;
        }
        if (!$next_prize_type_num || $next_prize_type_num<0){
            $arr1 = [];
        }
        return ['first_title'=>$arr,'second_title'=>$arr1];
    }

    public static function getFormatUserBox($themeid,$uid,$is_from_user = false,$config=[],$themyrecord=true)
    {
        $arr = [];
        //五锦中奖记录
        if ($is_from_user){
            $flag_res = Koi::getKoiFlagList($themeid,$uid,'default');
            foreach ($flag_res as $key => $val){
                $temp = [
                    'has'=>true,
                    'pre_txt'=>'免费',
                    'op_num'=>0,
                    'level'=>(int)0,
                    'img'=>$val['small_img'],
                    'box_name'=>'抽中',
                    'chest_name'=>$val['name'],
                    'btn'=>'开启'
                ];
                array_push($arr,$temp);
            }
        }
        //选择项礼品
        $themyrecord = Record::getLastGroupByUid($uid,$themeid);
        $select_gift = $themyrecord['address'];

        if ($select_gift && is_array($config['addressoptions'])){
            $options_key = array_search($select_gift,$config['addressoptions']);
            if (is_numeric($options_key)){
                $select_gift_img = $config['optionsimg'][$options_key];
                if ($select_gift_img && $select_gift){
                    $temp = [
                        'has'=>true,
                        'pre_txt'=>'',
                        'op_num'=>0,
                        'level'=>(int)0,
                        'img'=>$select_gift_img,
                        'box_name'=>$config['lz_package_name'],
                        'chest_name'=>$select_gift,
                        'btn'=>'开启'
                    ];
                    array_push($arr,$temp);
                }
            }
        }
        //读取后台礼包奖品
        if (is_array($config)){
            $temp_arr = [0,1,2,3,4];
            foreach ($temp_arr as $kk => $vv){
                if ($config['lingzeoptions'][$kk]){
                    $temp = [
                        'has'=>true,
                        'pre_txt'=>'',
                        'op_num'=>0,
                        'level'=>(int)0,
                        'img'=>$config['lingzeimg'][$kk],
                        'box_name'=>$config['lz_package_name'],
                        'chest_name'=>$config['lingzeoptions'][$kk],
                        'btn'=>'开启'
                    ];
                    array_push($arr,$temp);
                }
            }
        }

        if ($themyrecord){
            $prize_list = self::getAllPrize($themeid);
            array_multisort(array_column($prize_list,'level_sort'),SORT_ASC,$prize_list);
            foreach ($prize_list as $k => $v)
            {
                $has = self::getLtFlagItem($themeid,$uid,$v['lc_id']);
                if ($has){
                    $temp = [
                        'has'=>true,
                        'pre_txt'=>'开启',
                        'level'=>intval($v['level_sort']),
                        'op_num'=>$v['type_num'],
                        'img'=>$v['small_img'],
                        'box_name'=>$v['box_name'],
                        'chest_name'=>$v['chest_name'],
                        'btn'=>self::formatGetThirdOpFlag($themeid,$v['type'],$uid,$has['created_at'],$v['type_num'])
                    ];
                    array_push($arr,$temp);
                }else{
                    $temp = [
                        'has'=>false,
                        'pre_txt'=>'获得',
                        'level'=>$v['level_sort'],
                        'op_num'=>$v['type_num'],
                        'img'=>$v['small_img'],
                        'box_name'=>$v['box_name'],
                        'chest_name'=>$v['chest_name'],
                        'btn'=>'开启'
                    ];
                    array_push($arr,$temp);
                    break;
                }
            }

            if (!$is_from_user){
                array_multisort(array_column($arr,'has'),SORT_ASC,$arr);
            }

        }

        array_multisort(array_column($arr,'level'),SORT_DESC,$arr);
        if ($is_from_user){
            shuffle($arr);
        }
        //补充空位展位
        $arr_total = count($arr);
        if (!is_int($arr_total / 2) && $is_from_user){
            $temp = [
                'has'=>false,
                'pre_txt'=>'',
                'level'=>(int)0,
                'op_num'=>'',
                'img'=>'',
                'box_name'=>'',
                'chest_name'=>'',
                'btn'=>''
            ];
            array_push($arr,$temp);
        }
        return $arr;
    }

    public static function ApiAddOptionFlag($uid,$from_uid,$themeid,$type)
    {
        $fans_entry = self::getUserEntry($uid,$themeid);
        if ($fans_entry){
            $fan_lz_chest_sort = $fans_entry['lz_chest_sort'];
            $share_num = $fans_entry['share_num'];
            $friend_view_num = $fans_entry['friend_view_num'];
            $friend_order_num = $fans_entry['friend_order_num'];
            switch ($type){
                case 'share':
                    $new_share_num = $share_num+1;
                    $insert_data = [
                        'uid'=>$uid,
                        'themeid'=>$themeid,
                        'from_uid'=>$uid,
                        'type'=>'share'
                    ];
                    self::insertOpFlag($insert_data);
                    //跟新用户实例
                    $where_data = ['themeid'=>$themeid,'uid'=>$uid];
                    $update_data = [
                        'share_num'=>intval($new_share_num)
                    ];
                    self::editEntry($where_data,$update_data);
                    //关卡检测
                    self::checkLevelFlagAndPreFlag($themeid,$uid,'share',$new_share_num,$fan_lz_chest_sort);
                    break;
                case 'friend_view':
                    $new_friend_view_num = $friend_view_num+1;
                    $insert_data = [
                        'uid'=>$uid,
                        'themeid'=>$themeid,
                        'from_uid'=>$from_uid,
                        'type'=>'friend_view'
                    ];
                    self::insertOpFlag($insert_data);

                    //跟新用户实例
                    $where_data = ['themeid'=>$themeid,'uid'=>$uid];
                    $update_data = [
                        'friend_view_num'=>intval($new_friend_view_num)
                    ];
                    self::editEntry($where_data,$update_data);
                    //关卡检测
                    self::checkLevelFlagAndPreFlag($themeid,$uid,'friend_view',$new_friend_view_num,$fan_lz_chest_sort);
                    break;
                case 'friend_order':
                    $new_friend_order_num = $friend_order_num+1;
                    $insert_data = [
                        'uid'=>$uid,
                        'themeid'=>$themeid,
                        'from_uid'=>$from_uid,
                        'type'=>'friend_order'
                    ];
                    self::insertOpFlag($insert_data);

                    //跟新用户实例
                    $where_data = ['themeid'=>$themeid,'uid'=>$uid];
                    $update_data = [
                        'friend_order_num'=>intval($new_friend_order_num)
                    ];
                    self::editEntry($where_data,$update_data);
                    //关卡检测
                    self::checkLevelFlagAndPreFlag($themeid,$uid,'friend_order',$new_friend_order_num,$fan_lz_chest_sort);
                    break;
                default:
                    break;
            }
        }

    }
    //关卡奖品获得检测  通过关卡  前面关卡奖品自动通过获得
    public static function checkLevelFlagAndPreFlag($themeid,$uid,$op,$new_op_num,$level_sort)
    {
        (int)$next_level_sort = $level_sort+1;
        $next_prize = self::getPrizeItemByLevelSortAndOpNum($themeid,$next_level_sort);
        //判断下一关是否存在
        if ($next_prize){
            $type_num_total = self::getPrizeItemBySortTotalOpNum($themeid,$next_prize);
            $fans_entry = self::getUserEntry($uid,$themeid);
            $op = $next_prize['type'];
            switch ($op){
                case 'share':
                    $fans_op_total = (int)$fans_entry['share_num'];
                    break;
                case 'friend_view':
                    $fans_op_total = (int)$fans_entry['friend_view_num'];
                    break;
                case 'friend_order':
                    $fans_op_total = (int)$fans_entry['friend_order_num'];
                    break;
                default:
                    break;
            }

            if ($fans_op_total>=$type_num_total){
                //判断是否获取中奖记录
                $check_user_flag = self::getLtFlagItem($themeid,$uid,$next_prize['lc_id']);
                if (!$check_user_flag){
                    //插入中奖记录
                    $flag_insert_data = [
                        'uid'=>$uid,
                        'lc_id'=>$next_prize['lc_id'],
                        'status'=>1,
                        'themeid'=>$themeid
                    ];
                    self::insertLtFlag($flag_insert_data);

                    //更新用户的通过关卡
                    $where_data = ['themeid'=>$themeid,'uid'=>$uid];
                    $update_data = [
                        'lz_chest_sort'=>intval($next_prize['level_sort'])
                    ];
                    self::editEntry($where_data,$update_data);
                }
            }


        }
    }
}